#define  _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"

void SLInit(SL* p)
{
	p->arr = NULL;
	p->size = 0;
	p->capacity= 0;
}

void SLCheckCapacity(SL* p)
{
	if (p->size == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		SLDataType* tmp = (SLDataType*)realloc(p->arr, newcapacity * sizeof(SLDataType));
		assert(tmp);
		p->arr = tmp;
		p->capacity = newcapacity;
	}
}

void SLPrint(SL* p)
{

	for (int i = 0; i < p->size; i++)
	{
		printf("%d ", p->arr[i]);
	}
	printf("\n");
}

void SLPushback(SL* p, SLDataType x)
{
	assert(p);
	SLCheckCapacity(p);

	p->arr[p->size++] = x;
}

void SLPushfront(SL* p, SLDataType x)
{
	assert(p);
	SLCheckCapacity(p);

	for (int i = p->size; i > 0; i--)
	{
		p->arr[i] = p->arr[i - 1];
	}
	p->arr[0] = x;
	p->size++;
}

void SLPopback(SL* p)
{
	assert(p);
	assert(p->size);
	p->size--;
}

void SLPopfront(SL* p)
{
	assert(p);
	assert(p->size);
	for (int i = 0; i < p->size; i++)
	{
		p->arr[i] = p->arr[i + 1];
	}
	p->size--;
}

void SLInvert(SL* p, int lo, SLDataType x)
{
	assert(p);
	assert(lo >= 0 && lo <= p->size);
	for (int i = p->size; i > lo; i--)
	{
		p->arr[i] = p->arr[i - 1];
	}
	p->arr[lo] = x;
	p->size++;
}

void SLErase(SL* p, int lo)
{
	for (int i = lo; i < p->size; i++)
	{
		p->arr[i] = p->arr[i + 1];
	}
	p->size--;
}

int SLFind(SL* p, SLDataType x)
{
	for (int i = 0; i < p->size; i++)
	{
		if (p->arr[i] == x)
		{
			return 1;
		}
	}
	return -1;
}


void SLDestroy(SL* p)
{
	assert(p->arr);

	free(p->arr);
	p->arr = NULL;
}